﻿using System.ComponentModel;
using System.Configuration;
using System.Web.Configuration;
using System.Web.Security;

/// <summary>
/// Summary description for MemberShipPrivoiderManager
/// </summary>
[DataObject]
public class MemberShipPrivoiderManager
{
    [DataObjectMethod(DataObjectMethodType.Select)]
    public MemberShipSetting GetCurrentProvider()
    {
        MembershipProvider mp = Membership.Provider;
        
        var result = new MemberShipSetting
                         {
                             ApplicationName = mp.ApplicationName,
                             EnablePasswordReset = mp.EnablePasswordReset,
                             EnablePasswordRetrieval = mp.EnablePasswordRetrieval,
                             MaxInvalidPasswordAttempts = mp.MaxInvalidPasswordAttempts,
                             MinRequiredNonAlphanumericCharacters = mp.MinRequiredNonAlphanumericCharacters,
                             MinRequiredPasswordLength = mp.MinRequiredPasswordLength,
                             PasswordAttemptWindow = mp.PasswordAttemptWindow,
                             PasswordFormat = mp.PasswordFormat,
                             PasswordStrengthRegularExpression = mp.PasswordStrengthRegularExpression,
                             RequiresQuestionAndAnswer = mp.RequiresQuestionAndAnswer,
                             RequiresUniqueEmail = mp.RequiresUniqueEmail
                         };
        return result;
    }

    [DataObjectMethod(DataObjectMethodType.Update)]
    public void Save(MemberShipSetting mp)
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        var memberShip = (MembershipSection) config.GetSection("system.web/membership");
        string name = memberShip.DefaultProvider;
        ProviderSettings s = memberShip.Providers[name];
        s.Parameters.Clear();
        s.Parameters.Add("applicationName", mp.ApplicationName);
        s.Parameters.Add("enablePasswordReset", mp.EnablePasswordReset.ToString());
        s.Parameters.Add("enablePasswordRetrieval", mp.EnablePasswordRetrieval.ToString());
        s.Parameters.Add("maxInvalidPasswordAttempts", mp.MaxInvalidPasswordAttempts.ToString());
        s.Parameters.Add("minRequiredNonAlphanumericCharacters", mp.MinRequiredNonAlphanumericCharacters.ToString());
        s.Parameters.Add("minRequiredPasswordLength", mp.MinRequiredPasswordLength.ToString());
        s.Parameters.Add("passwordAttemptWindow", mp.PasswordAttemptWindow.ToString());
        s.Parameters.Add("passwordFormat", mp.PasswordFormat.ToString());
        s.Parameters.Add("passwordStrengthRegularExpression", mp.PasswordStrengthRegularExpression);
        s.Parameters.Add("requiresQuestionAndAnswer", mp.RequiresQuestionAndAnswer.ToString());
        s.Parameters.Add("requiresUniqueEmail", mp.RequiresUniqueEmail.ToString());
        config.Save();
    }

    #region Nested type: OrnamentMemberShipSetting

    public struct MemberShipSetting
    {
        //default:获取一个值，该值指示在成员资格数据存储区中存储密码的格式。Hashed 可选有：Hashed Encrypted ClearRole,        

        /// <summary>
        /// :user this Provider applicationName,default is host name
        /// </summary>
        public string ApplicationName { get; set; }

        public int MaxInvalidPasswordAttempts { get; set; }

        public int PasswordAttemptWindow { get; set; }

        public int MinRequiredNonAlphanumericCharacters { get; set; }

        public int MinRequiredPasswordLength { get; set; }

        public string PasswordStrengthRegularExpression { get; set; }

        /// <summary>
        /// 指示成员资格提供程序是否配置为允许用户重置其密码。
        /// </summary>
        public bool EnablePasswordReset { get; set; }

        public bool EnablePasswordRetrieval { get; set; }

        public bool RequiresQuestionAndAnswer { get; set; }

        public bool RequiresUniqueEmail { get; set; }

        public MembershipPasswordFormat PasswordFormat { get; set; }
    }

    #endregion
}